package com.zhuoyue.web.controller.system;


import com.zhuoyue.common.core.controller.ControllerResult;

import com.zhuoyue.common.core.domain.BatchResultDTO;
import com.zhuoyue.common.core.domain.ResultDTO;
import com.zhuoyue.common.core.domain.TblCountAndTimeDO;
import com.zhuoyue.common.core.domain.TblManagementDO;
import com.zhuoyue.common.enums.ResultCode;
import com.zhuoyue.system.domain.vo.TblReportFormsVo;
import com.zhuoyue.system.service.TblReportFormsService;
import com.zhuoyue.web.utils.OrgCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

import static com.zhuoyue.common.enums.RootCode.ORGCODE;


/**
 * 为图表模块提供各类数据
 */
@Controller
@Api(value = "数据报表接口", tags = "数据报表接口")
@RequestMapping("/jizi/reportform")
public class ReportFormsController {

    @Autowired
    private TblReportFormsService tblReportFormsService;


    @Autowired
    private OrgCodeUtils orgCodeUtils;

    /**
     * 1.计算每天,每周，每月注册的人数
     */
    @GetMapping("/getRegisterCount.do")
    @ResponseBody
    @ApiOperation(value = "计算每天,每周，每月注册的人数", notes = "计算每天,每周，每月注册的人数")
    public ControllerResult getRegister(HttpServletRequest request,
                                        HttpServletResponse response, HttpSession session) {

        ControllerResult controllerResultObj = new ControllerResult();


        TblManagementDO tblManagementDO = new TblManagementDO();


        if (StringUtils.isEmpty(orgCodeUtils.getOrgCode(request))) {
            tblManagementDO.setOrgCode("zyzn");
        }
        if (StringUtils.isEmpty(tblManagementDO.getOrgCode())) {
            controllerResultObj.setCode(ResultCode.PARAM_ERROR);
            controllerResultObj.setMsg("参数错误");
            return controllerResultObj;
        }

        try {
            TblReportFormsVo tblReportFormsBO = new TblReportFormsVo();
            if (!ORGCODE.equals(tblManagementDO.getOrgCode())) {
                tblReportFormsBO.setOrganizationCode(tblManagementDO.getOrgCode());
            }
            //一周之中七天时间的人数，七天
            BatchResultDTO<TblCountAndTimeDO> batchResultDTO1 = tblReportFormsService.getRegisterUserEveryWeek(tblReportFormsBO);
            if (batchResultDTO1.isFailed()) {
                controllerResultObj.setCode(ResultCode.PARAM_ERROR);
                controllerResultObj.setMsg(batchResultDTO1.getErrorDetail());
                return controllerResultObj;
            }

            //七周的每周的人数，七周
            BatchResultDTO<TblCountAndTimeDO> batchResultDTO2 = tblReportFormsService.getDeviceCountEveryWeek(tblReportFormsBO);
            if (batchResultDTO2.isFailed()) {
                controllerResultObj.setCode(ResultCode.PARAM_ERROR);
                controllerResultObj.setMsg(batchResultDTO2.getErrorDetail());
                return controllerResultObj;
            }

            //月总注册数，七个月
            BatchResultDTO<TblCountAndTimeDO> batchResultDTO3 = tblReportFormsService.getGateWayCountEveryWeek(tblReportFormsBO);
            if (batchResultDTO3.isFailed()) {
                controllerResultObj.setCode(ResultCode.PARAM_ERROR);
                controllerResultObj.setMsg(batchResultDTO3.getErrorDetail());
                return controllerResultObj;
            }

            List<List> peopleCount = new ArrayList<List>();
            peopleCount.add(0, batchResultDTO1.getModule());
            peopleCount.add(1, batchResultDTO2.getModule());
            peopleCount.add(2, batchResultDTO3.getModule());

            controllerResultObj.setSuccess(true);
            controllerResultObj.setData(peopleCount);
            controllerResultObj.setMsg("获取成功");
            System.out.println(controllerResultObj.getData());
        } catch (Exception e) {
            controllerResultObj.setMsg("推送接口异常");
            controllerResultObj.setCode(ResultCode.FAIL);
        }
        System.out.println("注册人数：" + controllerResultObj);
        return controllerResultObj;
    }

    /**
     * 2.数据统计模块
     */
    @GetMapping("/getDataCount.do")
    @ResponseBody
    @ApiOperation(value = "数据统计模块", notes = "数据统计模块")
    public ControllerResult getDataCount(HttpServletRequest request,
                                         HttpServletResponse response, HttpSession session) {


        ControllerResult controllerResultObj = new ControllerResult();
        String token = request.getParameter("token");
        try {
            TblManagementDO tblManagementDO = (TblManagementDO) session.getAttribute("user");

            if (StringUtils.isEmpty(tblManagementDO.getOrgCode())) {
                controllerResultObj.setSuccess(false);
                controllerResultObj.setCode(ResultCode.PARAM_ERROR);
                controllerResultObj.setMsg("组织为空不予进入");
                return controllerResultObj;
            }

            ResultDTO resultDTO = null;
            TblReportFormsVo tblReportFormsBO = new TblReportFormsVo();
            if (!ORGCODE.equals(tblManagementDO.getOrgCode())) {
                tblReportFormsBO.setOrganizationCode(tblManagementDO.getOrgCode());
                tblReportFormsBO.setRelationType("0");
            }

            resultDTO = tblReportFormsService.getUserAndDeviceAndGatewayAndMessageCount(tblManagementDO.getOrgCode(), tblReportFormsBO);
            if (resultDTO.isFailed()) {
                controllerResultObj.setSuccess(false);
                controllerResultObj.setCode(ResultCode.PARAM_ERROR);
                controllerResultObj.setMsg("获取失败");
                return controllerResultObj;
            }

            session.setAttribute("count1", resultDTO.getModule());
            controllerResultObj.setSuccess(true);
            controllerResultObj.setData(resultDTO.getModule());
            controllerResultObj.setMsg("获取成功");
            System.out.println(controllerResultObj.getData());
        } catch (Exception e) {
            controllerResultObj.setMsg("推送接口异常");
            controllerResultObj.setCode(ResultCode.FAIL);
        }
        return controllerResultObj;
    }


    /**
     * 3.设备柱状图
     *
     * @return
     */
    @GetMapping("/devicebar.do")
    public String devicebar() {
        System.out.println("1:访问总设备信息页面");
        return "senior/echarts/devicebar_update";
    }

    /**
     * 4.用户柱状图
     *
     * @return
     */
    @GetMapping("/userbar.do")
    public String bar() {
        System.out.println("1:访问总设备信息页面");
        return "senior/echarts/userbar";
    }

    /**
     * 4.1所有报表数据统计模块
     */
    @GetMapping("/getFormDataCount.do")
    @ResponseBody
    public ControllerResult getFormDataCount(HttpServletRequest request,
                                             HttpServletResponse response, HttpSession session,
                                             @RequestParam(value = "time", required = false) String time
            , @RequestParam(value = "type", required = false) String type
    ) {

        String sysDeptOrgCode = orgCodeUtils.getOrgCode(request);

        ControllerResult controllerResultObj = new ControllerResult();
        String token = request.getParameter("token");

        try {
            TblManagementDO tblManagementDO = new TblManagementDO();
            tblManagementDO.setOrgCode(sysDeptOrgCode);
            System.out.println();


            if (StringUtils.isEmpty(type)) {
                controllerResultObj.setSuccess(false);
                controllerResultObj.setMsg("类型不能为空获取");
                controllerResultObj.setCode(ResultCode.PARAM_ERROR);
                return controllerResultObj;
            }


            ResultDTO resultDTO = tblReportFormsService.getUserReportFormsData(type, time, tblManagementDO.getOrgCode());
            if (resultDTO.isFailed()) {
                controllerResultObj.setSuccess(false);
                controllerResultObj.setCode(ResultCode.PARAM_ERROR);
                controllerResultObj.setMsg("获取失败");
                return controllerResultObj;
            }
            controllerResultObj.setSuccess(true);
            controllerResultObj.setData(resultDTO.getModule());
            controllerResultObj.setMsg("获取成功");

        } catch (Exception e) {
            controllerResultObj.setMsg("推送接口异常");
            controllerResultObj.setCode(ResultCode.FAIL);
        }
        return controllerResultObj;
    }


    /**
     * 5.消息柱状图
     *
     * @return
     */
    @GetMapping("/messagebar.do")
    public String messagebar() {
        System.out.println("1:访问总设备信息页面");
        return "senior/echarts/messagebar_update";
    }

    /**
     * 5.报警柱状图
     *
     * @return
     */
    @GetMapping("/alarmbar.do")
    public String alarmbar() {
        System.out.println("1:访问总设备信息页面");
        return "senior/echarts/alarmbar_update";
    }

    /**
     * 6.网关柱状图
     *
     * @return
     */
    @GetMapping("/gatewaybar.do")
    public String gatewaybar() {
        System.out.println("1:访问总设备信息页面");
        return "senior/echarts/gatewaybar_update";
    }


}
